unit cuFuncs;

interface

uses Windows, SysUtils, Forms, Classes, Graphics, JPEG, Dialogs, Types, Math;

function InRange(theValue,Min,Max:integer):boolean;
function HighLow(theValue,Min,Max:integer):integer;

function GetEXEPath:String;
function FindFiles(const Path, Mask: string): TStringList;
function NowTime:string;
function NowDateTime:string;
function AddLeadingZeroes(nInt:integer; L:integer; C: char; Front: Boolean):string;
function BoolToStr2(AValue: boolean): string;
function Jpg2Bmp(sourceFilename:string;theWidth,theHeight:integer):TBitmap;

function IntToAlpha(num:integer):string;
function IntToLetter(num:integer):string;
function IntToColor(num:integer;Hidden:boolean = FALSE):integer;
function IntToRoman(num:integer):string;

function WeeksToStarDate(theTime:integer):real;

function IntToAnsiStr(X: Integer; Width: Integer = 0): AnsiString;

function RandFrom(fromValue,toValue:integer):integer;

function RandTest(theSeed:integer):string;

implementation

{
  Created by: Daniel Mark Avis

  Simple function to test a values range.
}
function InRange(theValue,Min,Max:integer):boolean;
begin
  result := FALSE;
  if (theValue >= Min)and(theValue <= Max) then
    result := TRUE;
end;

{
  Created by: Daniel Mark Avis

  Simple function to set a values range.
}
function HighLow(theValue,Min,Max:integer):integer;
begin
       if (theValue > Max) then result := Max
  else if (theValue < Min) then result := Min
  else result := theValue;
end;

{
  Created by: Daniel Mark Avis

  Simple function extract the application path.
}
function GetEXEPath:String;
begin
  Result:= ExtractFilePath(Application.ExeName);
end;

{
  Modified by: Daniel Mark Avis (unknown source)

  Returns a stringlist of all finds found along a path.

  Search for files with a mask; e.g.
  FindFiles('c:\data\', '*.mp3', true);
  FindFiles('d:\MyDocuments\', '*.doc', false);
  results the number of files found
}
function FindFiles(const Path, Mask: string): TStringList;
var
 FindResult : integer;
 SearchRec  : TSearchRec;
begin
 result := TStringList.Create;

 FindResult := FindFirst(Path + Mask, faAnyFile - faDirectory, SearchRec);
 while (FindResult = 0) do
 begin
   { do whatever you'd like to do with the files found }
   result.Add(SearchRec.Name);

   FindResult := FindNext(SearchRec);
 end;

 { free memory }
 FindClose(SearchRec);
end;

{
  Created by: Daniel Mark Avis

  Simple function to return the current time.
}
function NowTime:string;
begin
  result := TimeToStr( time );
end;

{
  Created by: Daniel Mark Avis

  Simple function to return the current date/time.
}
function NowDateTime:string;
begin
  result := DateToStr( date )+' '+TimeToStr( time );
end;

{
  Created by: Daniel Mark Avis

  Simple function to add leading zeros to a value (i.e to a time like 09:05, otherwise it would look like 9:5).
}
function AddLeadingZeroes(nInt:integer; L:integer; C: char; Front: Boolean):string;
 {Pads string s with char C to lenth l}
 var
   P: string;
   nStr : string;
 begin
    nStr := IntToStr( nInt );

    P := stringofchar(C, L - length(nStr));
    if Front then
       AddLeadingZeroes :=  P + nStr
    else
       AddLeadingZeroes :=  nStr + P;
end;

{
  Created by: Daniel Mark Avis

  Simple function to return the state of a boolean as a string.
}
function BoolToStr2(AValue: boolean): string;
begin
  result := 'WTF?!?!?';

  if (AValue = TRUE) then result := 'TRUE'
  else                    result := 'FALSE';
end; {--BoolToStr--}

{
  Modified by: Daniel Mark Avis (source unknown)

  Simple function to import a Jpg into a bitmap for the purpose of canvas manipulation.
}
function Jpg2Bmp(SourceFilename:string;theWidth,theHeight:integer):TBitmap;
var jpeg: TJpegImage;
begin
  Result := TBitmap.Create;

  try
    jpeg := TJpegImage.Create;
    try
      jpeg.LoadFromFile(SourceFilename);
      Result.Assign( jpeg );
    finally
      jpeg.Free;
    end;
    except
      ShowMessage('File Not Found: '+SourceFileName);
      Result.Free;
      jpeg.Free;//asa
    raise;
  end;
end;

{
  Created by: Daniel Mark Avis

  Converts a incremented value to a date time. in this format: 100.54
  Value preceding period is the multple of weeks of 52, the succeeding value is the current year below 52 weeks.
}
function WeeksToStarDate(theTime:integer):real;
// It will produce a time between x.0 to x.52
var theWeeks:integer;
var theYears:integer;
begin
  theYears := theTime div 52;
  theWeeks := theTime mod 52;

  result := theYears + ((theWeeks+1)/100);
end;

{
  Created by: Daniel Mark Avis

  Returns an Alpha word based on an inbound value.
}
function IntToAlpha(num:integer):string;
begin
  case Num of
    0 : result := 'Alpha';
    1 : result := 'Bravo';
    2 : result := 'Charlie';
    3 : result := 'Delta';
    4 : result := 'Echo';
    5 : result := 'Foxtrot';
    6 : result := 'Hotel';
    7 : result := 'Golf';
    8 : result := 'India';
    9 : result := 'Juliet';
    10 : result := 'Kilo';
    11 : result := 'Lima';
    12 : result := 'Mike';
    13 : result := 'November';
    14 : result := 'Oscar';
    15 : result := 'Papa';
    16 : result := 'Quebec';
    17 : result := 'Romeo';
    18 : result := 'Sierra';
    19 : result := 'Tango';
    20 : result := 'Uniform';
    21 : result := 'Victor';
    22 : result := 'Whiskey';
    23 : result := 'Xray';
    24 : result := 'Yankee';
    25 : result := 'Zulu';
    else result := IntToStr(num);//'Error:IntToAlpha()'; // Final solution
  end;
end;

{
  Created by: Daniel Mark Avis

  Returns an Char based on an inbound value.
}
function IntToLetter(num:integer):string;
begin
  case Num of
    0 : result := 'A';
    1 : result := 'B';
    2 : result := 'C';
    3 : result := 'D';
    4 : result := 'E';
    5 : result := 'F';
    6 : result := 'H';
    7 : result := 'G';
    8 : result := 'I';
    9 : result := 'J';
    10 : result := 'K';
    11 : result := 'L';
    12 : result := 'M';
    13 : result := 'N';
    14 : result := 'O';
    15 : result := 'P';
    16 : result := 'Q';
    17 : result := 'R';
    18 : result := 'S';
    19 : result := 'T';
    20 : result := 'U';
    21 : result := 'V';
    22 : result := 'W';
    23 : result := 'X';
    24 : result := 'Y';
    25 : result := 'Z';
    26 : result := '0';
    27 : result := '1';
    28 : result := '2';
    29 : result := '3';
    30 : result := '4';
    31 : result := '5';
    32 : result := '6';
    33 : result := '7';
    34 : result := '8';
    35 : result := '9';
    else result := IntToStr(num);
  end;
end;

{
  Created by: Daniel Mark Avis

  Returns an Roman Numeral based on an inbound value.
}
function IntToRoman(num:integer):string;
begin
  case Num of
    0 : result := 'I';
    1 : result := 'II';
    2 : result := 'III';
    3 : result := 'IV';
    4 : result := 'V';
    5 : result := 'VI';
    6 : result := 'VII';
    7 : result := 'VIII';
    8 : result := 'IX';
    9 : result := 'X';
    10 : result := 'XI';
    11 : result := 'XII';
    12 : result := 'XIII';
    13 : result := 'XIV';
    14 : result := 'XV';
    15 : result := 'XVI';
    16 : result := 'XVII';
    17 : result := 'XVIII';
    18 : result := 'XIX';
    19 : result := 'XX';
    20 : result := 'XXI';
    21 : result := 'XXII';
    22 : result := 'XXIII';
    23 : result := 'XXIV';
    24 : result := 'XXV';
    25 : result := 'XXVI';
    26 : result := 'XXVII';
    27 : result := 'XXVIII';
    28 : result := 'XXIX';
    29 : result := 'XXX';
    30 : result := 'XXXI';
    31 : result := 'XXXII';
    32 : result := 'XXXIII';
    33 : result := 'XXXIV';
    34 : result := 'XXXV';
    35 : result := 'XXXVI';
    else result := IntToStr(num);
  end;
end;

{
  Created by: Daniel Mark Avis

  Returns an Color based on an inbound value.
}
function IntToColor(num:integer;Hidden:boolean=FALSE):integer;
begin
  case Num of
    0 : result := $ff00ff00;
    1 : result := $ffa52a2a;
    2 : result := $ff0000ff;
    3 : result := $ff8a2be2;
    4 : result := $ff00ffff;
    5 : result := $ffdeb887;
    6 : result := $ff5f9ea0;
    7 : result := $ffd2691e;
    8 : result := $ffb8860b;
    9 : result := $ff8b008b;
    10 : result := $ffff8c00;
    11 : result := $ffffd700;
    12 : result := $ff008000;
    13 : result := $ffff69b4;
    14 : result := $fff0e68e;
    15 : result := $ff808000;
    16 : result := $ff2e8b57;
    17 : result := $ff9acd32;
    18 : result := $ffffffff;
    19 : result := $ffffffff;
    20 : result := $ffffffff;
    21 : result := $ffffffff;
    22 : result := $ffffffff;
    23 : result := $ffffffff;
    24 : result := $ffffffff;
    25 : result := $ffffffff;
    26 : result := $ffffffff;
    27 : result := $ffffffff;
    28 : result := $ffffffff;
    29 : result := $ffffffff;
    30 : result := $ffffffff;
    31 : result := $ffffffff;
    32 : result := $ffffffff;
    33 : result := $ffffffff;
//    34 : result := $ffffffff;
//    35 : result := $ffffffff;
    else
    begin
      if (Hidden) then result := $00ffffff
      else             result := $ffffffff;
    end;
  end;
end;

{
  Created by: Elkari - http://www.tek-tips.com/viewthread.cfm?qid=215607

  Rounds a value to specified significant digits. Unsure if this works.
}
function SignificantDigitRounding ( Number : Extended; Sig : Word; Direction : Boolean ) : Extended;
var Whole : Boolean;
{
}
begin
   if ( Number = int( Number ) ) then
      Whole := True
   else
      Whole := False;

   if ( Whole ) then
     Number := Number / power( 10, Sig - 1 )
   else
     Number := Number * power( 10, Sig - 1 );

    { Round Up }
    if ( Direction ) then Number := int( Number + 0.5 )
    else{ Round Down }    Number := int( Number - 0.5 );

    if ( Whole ) then
      Result := Number * power( 10, Sig - 1 )
    else
      Result := Number / power( 10, Sig - 1 );
end;

{
  Modified by: Daniel Mark Avis (Source Unknown)

  Returns an AnsiString from a integer.
}
function IntToAnsiStr(X: Integer; Width: Integer = 0): AnsiString;
begin
   Str(X: Width, Result);
end;

{
  Created by: Daniel Mark Avis

  Generates a random value between two values, even negative values.
}
function RandFrom(fromValue,toValue:integer):integer;
begin
  if (toValue > fromValue) then
    result := fromValue + Random(toValue - fromValue)
  else
    result := 0;
end;

{
  Created by: Martin Stoeckli - http://www.martinstoeckli.ch/welcome.html

  This function reads the file resource of "FileName" and returns the version number as formatted text.

  Sto_GetFmtFileVersion() = '4.13.128.0'
  Sto_GetFmtFileVersion('', '%.2d-%.2d-%.2d') = '04-13-128'
}
function Sto_GetFmtFileVersion(const FileName: String = '';
  const Fmt: String = '%d.%d.%d.%d'): String;
var
  sFileName: String;
  iBufferSize: DWORD;
  iDummy: DWORD;
  pBuffer: Pointer;
  pFileInfo: Pointer;
  iVer: array[1..4] of Word;
begin
  // set default value
  Result := '';
  // get filename of exe/dll if no filename is specified
  sFileName := FileName;
  if (sFileName = '') then
  begin
    // prepare buffer for path and terminating #0
    SetLength(sFileName, MAX_PATH + 1);
    SetLength(sFileName,
      GetModuleFileName(hInstance, PChar(sFileName), MAX_PATH + 1));
  end;
  // get size of version info (0 if no version info exists)
  iBufferSize := GetFileVersionInfoSize(PChar(sFileName), iDummy);
  if (iBufferSize > 0) then
  begin
    GetMem(pBuffer, iBufferSize);
    try
    // get fixed file info (language independent)
    GetFileVersionInfo(PChar(sFileName), 0, iBufferSize, pBuffer);
    VerQueryValue(pBuffer, '\', pFileInfo, iDummy);
    // read version blocks
    iVer[1] := HiWord(PVSFixedFileInfo(pFileInfo)^.dwFileVersionMS);
    iVer[2] := LoWord(PVSFixedFileInfo(pFileInfo)^.dwFileVersionMS);
    iVer[3] := HiWord(PVSFixedFileInfo(pFileInfo)^.dwFileVersionLS);
    iVer[4] := LoWord(PVSFixedFileInfo(pFileInfo)^.dwFileVersionLS);
    finally
      FreeMem(pBuffer);
    end;
    // format result string
    Result := Format(Fmt, [iVer[1], iVer[2], iVer[3], iVer[4]]);
  end;
end;


{
  Created by: Daniel Mark Avis

  Simple function to test RandSeed
}
function RandTest(theSeed:integer):string;
var R : integer;
begin
  RandSeed := theSeed;

  result := '';

  for R := 0 to 5 do
    result := result + IntToStr(Random(100))+' ';

  RandSeed := theSeed;

  result := result + ' / ';

  for R := 0 to 5 do
    result := result + IntToStr(Random(100))+' ';
end;

end.
